package log

import (
	"context"

	"yunzhan/log-server/internal/svc"
	"yunzhan/log-server/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RecordLogLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRecordLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RecordLogLogic {
	return &RecordLogLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RecordLogLogic) RecordLog(req *types.RecordLogRequest) (resp *types.RecordLogResponse, err error) {
	query := "INSERT INTO logs (component, service, message) VALUES (?, ?, ?)"
	_, err = l.svcCtx.Conn.Exec(query, req.Component, req.Service, req.Message)
	if err != nil {
		return &types.RecordLogResponse{Success: false}, err
	}
	return &types.RecordLogResponse{Success: true}, nil
}
